<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-06-25 09:36
 * Modified Time: 2019-06-25 09:36
 * Version: 2.0
 */

namespace App\Modules\Employee\Transformers;

use App\Lib\Utilities;
use League\Fractal\TransformerAbstract;
use tests\Mockery\Adapter\Phpunit\EmptyTestCase;

class EmployeeTransformer extends TransformerAbstract
{
	private $userInfo;
	private $roleInfo;
	private $storesInfo;
	private $statisticsByStoreOrBarber;
	private $isWorkOpened;
	private $roleType;
	private $currentStoreInfo;
	private $roleList;
	private $roleModule;

	public function __construct($userInfo = null, $roleInfo = null, $currentStoreInfo = null, $storesInfo = null, $statisticsByStoreOrBarber = null, $isWorkOpened = null, $roleType = null, $roleList = null, $roleModule = null)
	{
		$this->userInfo = $userInfo;
		$this->roleInfo = $roleInfo;
		$this->storesInfo = $storesInfo;
		$this->statisticsByStoreOrBarber = $statisticsByStoreOrBarber;
		$this->isWorkOpened = $isWorkOpened;
		$this->roleType = $roleType;
		$this->currentStoreInfo = $currentStoreInfo;
		$this->roleList = $roleList;
		$this->roleModule = $roleModule;
	}

	public function transform()
	{
		$result = empty($this->userInfo) ? [] : [
			'user_id'  => $this->userInfo->id,
			'nick'     => $this->userInfo->nick,
			'token'    => $this->userInfo->token,
			'portrait' => isset($this->userInfo->picture) ? $this->userInfo->picture->image : null
		];
		$result['current_role'] = $this->roleType;
		if (!empty($this->roleInfo)) {
			$result['store_id'] = $this->roleInfo['store_id'];
			$result['store_name'] = $this->roleInfo['store_name'];
//			$result['role'] = $this->roleInfo['role_bool'];
			$result['is_boss'] = $this->roleInfo['is_boss'];
			$result['is_barber'] = $this->roleInfo['is_barber'];
			$result['is_default_store'] = $this->roleInfo['is_default_store'];
		}

		if (!empty($this->currentStoreInfo)) {
			$result['store_info'] = [
				'store_id'   => $this->currentStoreInfo->id,
				'store_name' => $this->currentStoreInfo->name,
				'store_logo' => empty($this->currentStoreInfo->picture) ? null : $this->currentStoreInfo->picture->image
			];
		}

		if (!empty($this->storesInfo)) {
			foreach ($this->storesInfo as $v) {
				$result['stores'][] = [
					'store_id'   => $v['id'],
					'store_name' => $v['name'],
					'store_logo' => empty($v['picture']['image']) ? null : $v['picture']['image']
				];
			}
		}

		$result['role_list'] = $this->roleList;
		$result['modules'] = [];
		if (!empty($this->roleModule)) {
			foreach ($this->roleModule as &$v) {
				unset($v['pivot']);
			}
			unset($v);
			$result['modules'] = $this->roleModule;
		}

		if (config('constant.role.manager') == $this->roleType) {
			$result['is_open'] = is_array($this->isWorkOpened) ? in_array(0, array_column($this->isWorkOpened, 'is_satisfy')) ? 0 : 1 : $this->isWorkOpened;
			$result['conditions'] = $this->isWorkOpened;
		} else {
			$result['is_barber_open'] = $this->isWorkOpened;
		}

		if (!empty($this->statisticsByStoreOrBarber)) {
			$result['statistics'] = [     // 门店统计
			                                    'turnover'    => $this->statisticsByStoreOrBarber['turnOver'],
			                                    'appointment' => $this->statisticsByStoreOrBarber['orderAppointCount'],
			                                    'wait_time'   => $this->statisticsByStoreOrBarber['orderFinishedCount']
			];
		}

		return $result;
	}
}
